package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import data.PDMNode;

public class TaskEditor extends JDialog 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4296664984724639621L;
	private JTextField nameField;
	private JTextArea dependentField;
	private JTextField durField;
	
	private BaseFrame owner;
	private PDMNode node;
	
	public TaskEditor(BaseFrame owner)
	{
		this(owner, new PDMNode());
	}
	
	public TaskEditor(BaseFrame owner, PDMNode node)
	{
		super(owner, true);
		
		JPanel namePanel = new JPanel();
		JPanel durPanel = new JPanel();
		JPanel depPanel = new JPanel();
		JPanel top = new JPanel();
		JPanel bottom = new JPanel();
		this.node = node;
		this.owner = owner;
		
		JButton cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				TaskEditor.this.dispose();
			}
		});
		
		JButton submit = new JButton("Submit");
		submit.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				PDMNode wc = TaskEditor.this.node;
				wc.setDuration(new Integer(TaskEditor.this.durField.getText().trim()));
				wc.setNodeKey(TaskEditor.this.nameField.getText().trim());
				Scanner depBox = new Scanner(TaskEditor.this.dependentField.getText());
				depBox.useDelimiter(Pattern.compile("[ ,:;\\n]+"));
				try
				{
					while(depBox.hasNext())
					{
						String tok = depBox.next().trim();
						PDMNode dependentOn = TaskEditor.this.owner.getNode(tok);
						wc.getDependancies().add(dependentOn);
						dependentOn.getDependents().add(wc);
					}
					TaskEditor.this.owner.addNode(wc);
				} catch(NullPointerException ex) {
					JOptionPane.showMessageDialog(TaskEditor.this, "Could not resolve dependencies.", "Fatal Error", JOptionPane.ERROR_MESSAGE);
					System.exit(1);
					return;
				}
				TaskEditor.this.dispose();
			}
			
		});
		
		depPanel.setLayout(new BorderLayout());
		
		this.setBounds(owner.getX() + 60, owner.getY() + 20, 250, 200);
		
		this.setLayout(new BorderLayout());
		this.add(top, BorderLayout.NORTH);
		this.add(bottom, BorderLayout.SOUTH);
		
		String depStr = "";
		for(PDMNode n: node.getDependancies())
		{
			depStr += n.getNodeKey() + ", ";
		}
		if(depStr.length() > 2)
		{
			depStr = depStr.substring(0, depStr.length() - 2);
		}
		
		nameField = new JTextField(node.getNodeKey());
		durField = new JTextField(((Integer) node.getDuration()).toString());
		dependentField = new JTextArea(depStr);
		
		JLabel nameLabel = new JLabel("Name: ");
		JLabel durLabel = new JLabel("Duration: ");
		JLabel dependencies = new JLabel("Dependent on:");
		
		top.setLayout(new GridLayout(2,1));
		
		namePanel.setLayout(new BorderLayout());
		namePanel.add(nameLabel, BorderLayout.WEST);
		namePanel.add(nameField);
		
		durPanel.setLayout(new BorderLayout());
		durPanel.add(durLabel, BorderLayout.WEST);
		durPanel.add(durField);
		
		depPanel.add(dependencies, BorderLayout.NORTH);
		depPanel.add(dependentField);

		top.add(namePanel);
		top.add(durPanel);
		this.add(depPanel);
		
		bottom.add(submit);
		bottom.add(cancel);
		
	}
}
